package com.wkbb.common.utils;

import com.wkbb.common.enums.ProfilesActiveEnum;

import java.text.NumberFormat;
import java.util.UUID;

public class UuIdUtil {

    /**
     * 生成用户编号
     */
    public  static String getUserCodeByUUId() {
        //s
        synchronized(UuIdUtil.class){
            int machineId = 1;//最大支持1-9个集群机器部署

            int hashCodeV = UUID.randomUUID().toString().hashCode();
            if (hashCodeV < 0) {//有可能是负数
                hashCodeV = -hashCodeV;
            }

//      0 代表前面补充0
//      19 代表长度为4
//      d 代表参数为正数型
            return machineId + String.format("%015d", hashCodeV);
        }
    }

    /**
     * 生成用户编号
     */
    public static String getSessionIdByUUId() {

        int machineId = 2;//最大支持1-9个集群机器部署

        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }

//      0 代表前面补充0
//      19 代表长度为4
//      d 代表参数为正数型
        return machineId + String.format("%019d", hashCodeV);
    }

    /**
     * 生成getGroupIds自动补位
     */
    private static final int groupIdLength = 10; //微众groupid长度默认是10
    public static String getGroupIds(String profilesActive,String orgId) {
        if(StringUtil.isEmptyIgnoreBlank(orgId) || orgId.length()>=groupIdLength){
            return orgId;
        }
        String headerArg="";
        String repairArg="";
        if(profilesActive.equals(ProfilesActiveEnum.DEV.getDesc())){
            headerArg=ProfilesActiveEnum.DEV.getValue().toString();
        }else if(profilesActive.equals(ProfilesActiveEnum.GRAY.getDesc())){
            headerArg=ProfilesActiveEnum.GRAY.getValue().toString();
        }else if(profilesActive.equals(ProfilesActiveEnum.TEST.getDesc())){
            headerArg=ProfilesActiveEnum.TEST.getValue().toString();
        }else{
            headerArg=ProfilesActiveEnum.PRO.getValue().toString();
        }
        //待测试数据
//        int d=10;
        if(groupIdLength>(headerArg+orgId).length()) {
            for(int c=0;c<groupIdLength-(headerArg+orgId).length();c++) {
                repairArg+= "0";
            }
        }
        return headerArg+repairArg+orgId;
    }


    /**
     * 生成getVisitorsGroupIds访客组名称自动补位
     */
    public static String getVisitorsGroupIds(String profilesActive,String orgId) {
        String headerArg="";
        String repairArg="";
        if(profilesActive.equals(ProfilesActiveEnum.DEV.getDesc())){
            headerArg="VS"+ProfilesActiveEnum.DEV.getValue().toString();
        }else if(profilesActive.equals(ProfilesActiveEnum.GRAY.getDesc())){
            headerArg="VS"+ProfilesActiveEnum.GRAY.getValue().toString();
        }else if(profilesActive.equals(ProfilesActiveEnum.TEST.getDesc())){
            headerArg="VS"+ProfilesActiveEnum.TEST.getValue().toString();
        }else{
            headerArg="VS"+ProfilesActiveEnum.PRO.getValue().toString();
        }
        //待测试数据
        int d=10;
        if(d>(headerArg+orgId).length()) {
            for(int c=0;c<d-(headerArg+orgId).length();c++) {
                repairArg+= "0";
            }
        }
        return headerArg+repairArg+orgId;
    }

    /**
     * 生成虚拟手机号
     */
    public static String getPhoneByUUId(int machineId) {

        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }

        return machineId + String.format("%010d", hashCodeV);
    }

    public static void main(String args[])
    {
        System.out.println(getGroupIds("33","1"));
    }

}